var database = require('./database'),
	util = require('util'),
	gcm = require('node-gcm'),
	config = require('../config');
/**
 * Notification related methods
 */
 
 /*
 * Notify all android devices with this given type and payload, test purpose
 */
exports.notify = function(type, payload, callback) {
	console.log("Core/notifications.js Function 'notify' was called, type: "+type+" pl: "+payload);

	var respond = function(regIds) {
		var message = new gcm.Message();
		var sender = new gcm.Sender(config.gcm_apikey);

		// Optional
		message.addData('type', type);
		message.addData('payload', payload);
		message.collapseKey = type;
		message.delayWhileIdle = false;
		message.timeToLive = 3;

		/**
		 * Parameters: message-literal, registrationIds-array, No. of retries, callback-function
		 */
		sender.send(message, regIds, 4, function(err, result) {
			console.log(result);
		});
		
		if(callback != null) {
			callback(JSON.stringify(regIds))
		}
	}
	
	database.getDevicesIDs(respond);
};
